import React from 'react';
import {Transfer} from 'antd';

export default class App extends React.Component {
  state = {
    dataSource: [],
    targetKeys: [],
  };

  componentDidMount() {
    this.getMock();
  }

  getMock = () => {
    const targetKeys = [];
    const dataSource = [];
    for (let i = 0; i < 20; i++) {
      const data = {
        key: i.toString(),
        title: `内容${i + 1}`,
        description: `描述与内容${i + 1}`,
        chosen: Math.random() * 2 > 1,
      };
      if (data.chosen) {
        targetKeys.push(data.key);
      }
      dataSource.push(data);
    }
    this.setState({dataSource, targetKeys});
  };

  handleChange = (targetKeys, direction, moveKeys) => {
    console.log(targetKeys, direction, moveKeys,"这是手动改变的");
    console.log(this.state,"this.state");
    this.setState({targetKeys});
  };

  renderItem = item => {
    const customLabel = (
      <span className="custom-item">
        {item.title} - {item.description}
      </span>
    );

    return {
      label: customLabel, // for displayed item
      value: item.title, // for title and filter matching
    };
  };

  render() {
    return (
      <Transfer
        locale={{itemUnit: '项', itemsUnit: '项', searchPlaceholder: '请输入搜索内容'}}
        titles={['待选用户', '已选用户']}
        listStyle={{width: 300, height: 300,}}
        dataSource={this.state.dataSource}
        targetKeys={this.state.targetKeys}
        onChange={this.handleChange}
        render={this.renderItem}
      />
    );
  }
}
